var app = angular.module('myApp', ['ngGrid']);
app.controller('subdealer', function($scope, $http) {
    $scope.filterOptions = {
        filterText: "",
        useExternalFilter: true
    };
    $scope.totalServerItems = 0;
    $scope.pagingOptions = {
        pageSizes: [50, 100],
        pageSize: 50,
        currentPage: 1
    };  
    csvOpts=0;
    $scope.setPagingData = function(data, page, pageSize){	
        var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
        $scope.myData = pagedData;
        $scope.totalServerItems = data.length;
        if (!$scope.$$phase) {
            $scope.$apply();
        }
    };
    $scope.getPagedDataAsync = function (pageSize, page, searchText) {
        setTimeout(function () {
            var data;
            if (searchText) {
                var ft = searchText.toLowerCase();
                $http.get("subdealerdata.php").success(function (largeLoad) {		
                    data = largeLoad.filter(function(item) {
                        return JSON.stringify(item).toLowerCase().indexOf(ft) != -1;
                    });
                    $scope.setPagingData(data,page,pageSize);
                });            
            } else {
                $http.get("subdealerdata.php").success(function (largeLoad) {
                    $scope.setPagingData(largeLoad,page,pageSize);
                });
            }
        }, 100);
    };

		$scope.refreshGrid = function() {
			$scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
		};	
	
	
    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
	
    $scope.$watch('pagingOptions', function (newVal, oldVal) {
        if (newVal !== oldVal && newVal.currentPage !== oldVal.currentPage) {
          $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
        }
    }, true);
    $scope.$watch('filterOptions', function (newVal, oldVal) {
        if (newVal !== oldVal) {
          $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
        }
    }, true);

    $scope.gridOptions = {
        data: 'myData',
		columnDefs: [{field:'Sr_NO', displayName:'Sr No',width:60, cellTemplate: '<div style="text-align:left;" class="ngCellText">{{row.getProperty(col.field)}}</div> '},{field:'subname', displayName:'Sub-Agent Name',width:350, cellTemplate: '<div style="text-align:left;" class="ngCellText">{{row.getProperty(col.field)}}</div> '},{field:'Address', displayName:'Address', width:350 , cellTemplate: '<div style="text-align:left;" class="ngCellText">{{row.getProperty(col.field)}}</div> '},{field:'City', displayName:'City', width:90 ,cellTemplate: '<div style="text-align:left;" class="ngCellText">{{row.getProperty(col.field)}}</div> '},
					 {displayName: 'Action', cellTemplate: '<img src=glyphicons_bin.png title="Delete" name="Delete" onclick="deletesubdealer(\'{{row.entity.subname}} \')" value="Delete"> | <img src=glyphicons_edit.png title="Edit" name="Edit" onclick="editsubdealer(\'subdealeredit.php?name={{row.entity.subid}}&up=true \')" value="Edit">'}],
		plugins: [new ngGridCsvExportPlugin(csvOpts)],
        showGroupPanel: true,
        enablePaging: true,
        showFooter: true,
        totalServerItems:'totalServerItems',
        pagingOptions: $scope.pagingOptions,
        filterOptions: $scope.filterOptions,
		jqueryUITheme: true
    };
});